package stackandqueue.stack.linkedstack;

import stackandqueue.stack.inter.Stack;

public class LinkedStack<T> implements Stack<T> {
    private StackNode<T> top;
    private int count;

    public LinkedStack() {
        this.top = null;
        count = 0;
    }

    @Override
    public void clearStack() {
        top = null;
        count = 0;
    }

    @Override
    public boolean stackEmpty() {
        return (top == null);
    }

    @Override
    public T getTop() {
        return top.getData();
    }

    @Override
    public void push(T e) {
        StackNode temp = new StackNode();
        temp.setData(e);
        temp.setNext(top);
        top = temp;
        count++;
    }

    @Override
    public T pop() {
        StackNode<T> p = top;
        top = top.getNext();
        p.setNext(null);
        count--;
        return p.getData();
    }

    @Override
    public int stackLength() {
        return count;
    }
}
